
 var rules = {
            email: {
                identifier: 'email',
        rules: [
            {
                type: 'empty',
                prompt: "邮箱不能为空"
            }, {
                type: 'email',
                prompt: '邮箱格式有误'
            }
        ]
    },
    userName: {
        identifier: 'userName',
        rules: [
            {
                type: 'empty',
                prompt: '用户名不能为空'
            }, {
                type: 'regExp[/^[a-z0-9]*/]',
                prompt: '用户名只能数字与字母'
            }, {
                type: 'regExp[/^[a-z0-9]{4,16}$/]',
                prompt: '长度6-16位'
            }
        ]
    },
    password: {
        identifier: 'password',
        rules: [
            {
                type: 'empty',
                prompt: '密码不能为空'
            }, {
                type: 'regExp[/^.{6,16}$/]',
                prompt: '密码长度为6-16位'
            }, {
                type: 'regExp[/^(?![0-9]+$)/]',
                prompt: '密码不能全为数字'
            }, {
                type: 'regExp[/^(?![a-zA-z]+$)/]',
                prompt: '密码不能全为字符'
            }, {
                type: 'regExp[/^(?![!@#$%^&*]+$)/]',
                prompt: '密码不能全为特殊字符'
            }, {
                type: 'regExp[/^[a-zA-Z0-9!@#$%^&*]+$/]',
                prompt: '密码包含非法字符'
            }
        ]
    },
    password2: {
        identifier: 'password2',
        rules: [
            {
                type: 'empty',
                prompt: '密码不能为空'
            },
            {
                type: 'match[password]',
                prompt: "密码不一致"
            }
        ]
    },
    nickName: {
        identifier: 'nickName',
        rules: [
            {
                type: 'empty',
                prompt: '昵称不能为空'
            }
        ]
    },
    hasRead: {
        identifier: 'hasRead',
        rules: [
            {
                type: 'checked',
                prompt: "请勾选用户协议"
            }
        ]
    }
};

$(document).ready(function () {
	//初始化验证
    $("form").form({
        on: 'blur',
        fields: rules,
        inline: true,
        onValid: function () {
        	var name = $(this).attr('name');
        	//只验证userNmae、email、nickName的唯一性
        	if(name == "userName" || name == "email" || name == "nickName"){
                ajaxValidate(name, $(this).val());
        	}
        }
    });
    
    //初始化下拉菜单
    $(".ui.selection.dropdown").dropdown();
    
    
    $("#selection-province").dropdown({
    	apiSettings: {url: "../../p/area/query.jhtml?pid=0"},
    	fields: {remoteValues : 'data',name:'name',value: 'id' },
    	onHide: function(){
		   $("#selection-city").dropdown({
		    	apiSettings: {url: "../../p/area/query.jhtml?pid=" + $("#selection-province").dropdown("get value")},
		    	fields: {remoteValues : 'data',name:'name',value: 'id' },
		    	onHide: function(){
		 		   $("#selection-county").dropdown({
				    	apiSettings: {url: "../../p/area/query.jhtml?pid=" + $("#selection-city").dropdown("get value")},
				    	fields: {remoteValues : 'data',name:'name',value: 'id' }
				    });
		    	},
		    	onChange: function(value, text, $choice){
		    		$("#selection-county").dropdown("clear");
		    	}
		    });
    	},
    	onChange: function(value, text, $choice){
    		$("#selection-city").dropdown("clear");
    		$("#selection-county").dropdown("clear");
    	}
    });
});


/**
 * 验证值得唯一性
 * @param selector 输入框的name或者id对应的值
 * @param value 对应输入框当前值
 */
function ajaxValidate(selector, value) {
	var flag = false;
    $.ajax({
        url: "unique.jhtml",
        type: 'post',
        data: {name: selector, value: value},
        dataType: 'json',
        async:false,
        success: function (data) {
            if (data['code'] == "L001") {
                $("input[name=" + selector + "]").next().css("visibility", "visible");
                $("form").form("remove prompt", selector, data['msg']);
                flag = true;
            } else {
                $("input[name=" + selector + "]").next().css("visibility", "hidden");
                $("form").form("add prompt", selector, data['msg']);
                flag = false;
            }
        }
    });
    return flag;
}

function submitForm(url) {

	$(".dimmer").addClass("active");
	
    if (!ajaxValidate("email", $("input[name=email]").val())) {
        return false;
    }

    if (!ajaxValidate("userName", $("input[name=userName]").val())) {
        return false;
    }

    if (!ajaxValidate("nickName", $("input[name=nickName]").val())) {
        return false;
    }


    var flag = $("form").form({
        on: 'blur',
        fields: rules,
        inline: true
    }).form("is valid");
    
    
    if(!flag){
    	$(".dimmer").removeClass("active");
        $(".error.message").addClass("visible").text("注册信息有误");
        return false;
    }
    
    $.ajax({
    	url: url,
    	type: 'post',
    	data: $("form").serialize(),
    	dataType: 'json',
    	success: function(data){
    		if(data['code'] == '1'){
    			$(".loader").text("注册成功，请查看邮箱");
    		}else{
    			$(".dimmer").removeClass("active");
				$(".error.message").addClass("visible").text(data['msg']);
    		}
    	}
    });
}




